﻿/*
  +++++++++++++++++++++++++++++++++++++++++++
  +  Description: 用户信息 管理
  +  Created by 王大宸 on 2020-10-05 23:28:38
  +  Created with IntelliJ IDEA.
  ++++++++++++++++++++++++++++++++++++++++++++
 */

var path = "/base/user";

layui.use(['table', 'laypage'], function () {
    table = layui.table;
    layPage = layui.laypage;
    form = layui.form;

    /* 初始化表格 */
    initTable();

    // 搜索
    $.searchTable.searchBtn();

    //监听状态操作
    form.on('switch(statusTemplate)', function (obj) {
        updateStatus(this.value, obj.elem.checked);
    });

    //监听行工具事件
    table.on('tool(baseUserLay)', function (obj) {
        let data = obj.data;
        if ('del' === obj.event) {
            let removeUrl = path + '/' + data.id;
            $.http.remove(removeUrl, data.name);
        } else if ('edit' === obj.event) {
            let editUrl = path + '/edit/' + data.id;
            $.modal.open('修改', editUrl, '650', '580');
        } else if ('openAvatar' === obj.event) {
            let avatarUrl = data.avatar;
            if (null != avatarUrl && '' !== avatarUrl && undefined !== avatarUrl) {
                $.modal.openImg(data.name, avatarUrl, '500', '500');
            } else {
                $.modal.openImg(data.name, "/static/home/images/avatar.jpg", '500', '500');
            }
        } else if ('see' === obj.event) {
            let seeUrl = path + '/see/' + data.id;
            $.modal.see('查看', seeUrl, '650', '580');
        } else if ('power' === obj.event) {
            let powerUrl = path + '/power/' + data.id;
            $.modal.iframe('角色权限', powerUrl, '350', '500');
        } else if ('issue_power' === obj.event) {
            let powerUrl = path + '/issue/power/' + data.id;
            $.modal.iframe('权限下发', powerUrl, '350', '500');
        }
    });

});

/* 加载表格 */
function initTable() {
    let name = $("#name").val();
    $.table.init({
        url: path + "/page",
        where: {
            name: name
        },
        cols: [[
            {field: 'username', title: '登录账户', align: "center"},
            {field: 'name', title: '姓名', align: "center"},
            {
                field: 'avatar', title: '头像', width: 100, align: "center", event: "openAvatar", templet: function (d) {
                    let avatar = d.avatar;
                    if ($.string.isEmpty(avatar)) {
                        return '<img style="width:100%;height:100%;cursor:pointer;" src="/static/home/images/avatar.jpg" />';
                    } else {
                        return '<img style="width:100%;height:100%;cursor:pointer;" src="' + d.avatar + '" />';
                    }
                }
            },
            {field: 'telPhone', title: '电话号码', width: 120, align: "center"},
            {field: 'email', title: '电子邮箱', align: "center"},
            {field: 'sex', title: '性别', width: 100, align: "center"},
            {field: 'status', title: '状态', width: 100, align: "center", templet: "#statusTemplate"},
            {fixed: 'right', title: '操作', toolbar: '#toolbarHandle', width: 500, align: "center"}
        ]],
    });
}

/* 添加按钮 */
function saveBaseUser() {
    let addUrl = path + '/add';
    $.modal.open("添加", addUrl, "650", "600");
}

/* 修改用户状态 */
function updateStatus(id, value) {
    let status = '1';
    if (value) {
        status = '0';
    }
    let url = path + "/" + id + "/status/" + status;
    $.http.putAjax(url, {}, function (res) {
        if (WEB_STATUS.SUCCESS === res.status) {
            let msg = '账号解除冻结成功';
            if (value) {
                msg = "冻结账号成功";
            }
            $.msg.msgSuccess(msg);
        } else {
            initTable();
            $.msg.msgWarning(res.msg)
        }
    })
}

